<?php

/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2018/1/23
 * Time: 22:06
 */
namespace app\admin\controller;

use app\common\helper\AdminInfoHelper;
use app\common\helper\ResponseHelper;
use app\common\model\Admin;
use app\common\service\AdminService;
use think\App;
use think\facade\Request;
use think\facade\Session;

abstract class Auth extends Base
{

    /**
     * Auth constructor.
     * @param AdminInfoHelper $adminInfoHelper
     * @param App|null $app
     */
    public function __construct(AdminInfoHelper $adminInfoHelper, App $app = null)
    {
        parent::__construct($app);
        return $this->initAdmin($adminInfoHelper);
    }

    /**
     * 初始化admin
     * @author liud
     * @param AdminInfoHelper $adminInfoHelper
     */
    private function initAdmin(AdminInfoHelper $adminInfoHelper)
    {
        $adminId = Session::get('aid');
        if (!$adminId) {
            $this->redirect('/admin/login');
            return;
        }
        $admin = AdminService::getAdminById($adminId);
        if(!$admin){
            $this->redirect('/admin/login');
            return ;
        }
        $this->loginId = $adminId;
        $this->admin = $admin;
        $admin = $this->admin->toArray();
        foreach ($admin as $k => $v) {
            if (property_exists($adminInfoHelper, $k)) {
                $adminInfoHelper->$k = $v;
            }
        }
    }
}